package com.hup.joystick.view.volumeMenu;

import com.hup.joystick.constant.DeviceKey.StandardKeys;
import com.hup.joystick.view.customView.BaseMenuWindow;
import com.hup.joystick.view.quickMenu.setting.QuickMenuSettingDialog;
import com.hup.utils.commons.hardware.HRobotJni;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.swing.frame.SingletonWindowHelper;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Lazy;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/**
 * 音量调节JWindow,展示时,不会影响以全屏的程序
 *
 * @author hugan
 * @date 2020/11/18
 */
@Log4j2
@Lazy
@org.springframework.stereotype.Component
public class VolumeMenu extends BaseMenuWindow {

    private final boolean isPreview;
    private VolumeMenuForm form;
    private boolean onPressArrow;

    public static void showFrame() {
        BaseMenuWindow.disposeShowingWindows();
        QuickMenuSettingDialog quickMenuSettingFrame = SingletonWindowHelper.getWindow(QuickMenuSettingDialog.class);
        boolean isPreview = quickMenuSettingFrame != null;
        log.info("isPreview={}", isPreview);

        VolumeMenu volumeMenu;
        if (!isPreview) {
            volumeMenu = SpringContext.getBean(VolumeMenu.class);
        } else {
            volumeMenu = new VolumeMenu(quickMenuSettingFrame);
            SingletonWindowHelper.addWindow(volumeMenu);
        }

        //每次都要刷新分辨率
        HRobotJni.createGlobalVolumeUtil();
        volumeMenu.updateVolume(HRobotJni.getVolume());
        volumeMenu.updateMute(HRobotJni.isMute());

        volumeMenu.showMenuWindow();
    }

    private VolumeMenu() {
        this(null);
    }

    /**
     * @param previewOwner 预览owner,解决ownerDialog阻塞当前窗口
     */
    private VolumeMenu(Window previewOwner) {
        super(previewOwner, "音量设置");
        isPreview = previewOwner != null;
        initView();
        initListener();
    }

    private void initView() {
        form = new VolumeMenuForm();
        setContentPane(form.root);

        LookThemeUtil.fixFontScale(form.txtTip);
        initTransparentBg(form.btnMute, form.txtVolume);
        setAlwaysOnTop(true);
    }

    private void initListener() {
        //点击背景退出
        form.root.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                VolumeMenu.this.dispose();
            }
        });
        form.slVolume.addChangeListener(e -> {
            if (onPressArrow) return;//getValueIsAdjusting很废,外部设置后会被其他自身逻辑修改
            log.debug("e={}", e);
            int value = form.slVolume.getValue();
            form.txtVolume.setText("" + value);
            updateMute(false);
            HRobotJni.setVolume(value);
        });
        form.btnMute.setActionListener(e -> {
            boolean mute = form.btnMute.isSelected();
            updateMute(mute);
            HRobotJni.setMute(mute);
        });
    }

    @Override
    public boolean isSingleton() {
        return false;//阻止SpringBoot创建本类时,添加对象到SingletonWindowHelper,
    }

    @Override
    public void dispose() {
        super.dispose();
        if (isPreview) SingletonWindowHelper.removeWindow(this);
        HRobotJni.releaseGlobalVolumeUtil();
    }

    private void updateVolume(int volume) {
        log.debug("volume={}", volume);
        form.slVolume.setValue(volume);
        form.txtVolume.setText("" + volume);
    }

    private void updateMute(boolean mute) {
        form.btnMute.setSelected(mute);
    }

    /**
     * @param key {@link StandardKeys#ARROW_N}等
     * @param isLongPress true-长按事件,音量调节步长为10;false-音量调节步长为1
     */
    public void onPressJoyArrow(String key, boolean isLongPress) {
        if (StandardKeys.ARROW_N.key.equals(key) || StandardKeys.ARROW_S.key.equals(key)) {
            boolean mute = !HRobotJni.isMute();
            updateMute(mute);
            HRobotJni.setMute(mute);
            return;
        }
        int volume = HRobotJni.getVolume();
        int step = !isLongPress ? 1 : 10;
        if (StandardKeys.ARROW_W.key.equals(key)) {
            volume -= step;
        } else if (StandardKeys.ARROW_E.key.equals(key)) {
            volume += step;
        }
        onPressArrow = true;
        volume = HRobotJni.setVolume(volume);
        updateVolume(volume);
        updateMute(false);
        onPressArrow = false;
    }

}
